﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ATMsystem.Form1;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ATMsystem
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        private Account acnt = new Account();

        private void button2_Click(object sender, EventArgs e)//余额查询
        {

            string s1 = String.Format("余额为：{0}", acnt.Balance);
            MessageBox.Show(s1);
        }

        public Account Acnt { get { return acnt; } set { acnt = value; } }



       


        public class BigMoneyArgs : EventArgs
        {
            public Account account;
            public int fetchedmoney;
        }
 
        public delegate void BigMoneyFetchedEventHandler(object sender, BigMoneyArgs e);
        public static event BigMoneyFetchedEventHandler BigMoneyFetched;
        public static void alertBigMoney(object sender, BigMoneyArgs e)
        {
            
            MessageBox.Show("在ATM取款不允许超过10000");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)//存款
            {
                acnt.saveMoney(Convert.ToInt32(textBox1.Text));
                MessageBox.Show("存款成功！");
                clearScreen();
            }
            else if (radioButton2.Checked)//取款
            {
                if (Convert.ToInt32(textBox1.Text) > acnt.Balance)
                {                         
                    MessageBox.Show("余额不足，取款失败！");
                    clearScreen();
                }
                    
                else if (Convert.ToInt32(textBox1.Text) > 10000)
                {
                    BigMoneyArgs args = new BigMoneyArgs();
                    args.account = acnt;
                    args.fetchedmoney = Convert.ToInt32(textBox1.Text);
                    BigMoneyFetched(this, args);
                    clearScreen();
                    return;
                }
                else
                {
                    try
                    {
                        acnt.withdrawMoney(Convert.ToInt32(textBox1.Text));
                        MessageBox.Show("取款成功！");
                    }
                    catch (BadCashException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    finally
                    {
                        clearScreen();
                    }
                }

            }
            else MessageBox.Show("请选择所需服务！");

        }
        public void clearScreen()
        {
            textBox1.Text = "";
            radioButton1.Checked = false;
            radioButton2.Checked = false;
        }
    }
}
